<?php
// +----------------------------------------------------------------------
// | 地址
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v3;

use app\event\store\Address as AddressEvent;

class Address extends Common
{

    /**
     * 删除地址
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            $address = $this->user->addresses()->where('id', $this->request->post('id'))->find();
            if ($address) {
                if ($address['is_default'] == 1) {
                    // 删除了默认地址
                    $address->is_default = 0;
                    $this->user->addresses()->where('id', '<>', $this->request->post('id'))->limit(1)->setField('is_default', 1);
                }
                $address->status = -2;
                if ($address->save()) {
                    // 其他改为非默认
                    $data['code'] = 0;
                    $data['message'] = '删除成功';
                } else {
                    $data['code'] = 1;
                    $data['message'] = '删除异常';
                }
            } else {
                $data['code'] = 1;
                $data['message'] = '未发现当前地址';
            }
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return $this->autoAjax($data);
    }

    public function setDefault()
    {
        if ($this->request->isPost()) {
            $address = $this->user->addresses()->where('id', $this->request->post('id'))->find();
            if ($address && 0 == $address['is_default']) {
                $address['is_default'] = 1;
                if ($address->save()) {
                    // 其他改为非默认
                    $this->user->addresses()->where('id', '<>', $this->request->post('id'))->setField('is_default', 0);
                    $data['code'] = 0;
                    $data['message'] = '修改成功';
                } else {
                    $data['code'] = 1;
                    $data['message'] = '修改异常';
                }
            } else {
                $data['code'] = 1;
                $data['message'] = '未更新';
            }
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return $this->autoAjax($data);
    }

    /**
     * 我的所有地址
     */
    public function gets()
    {
        $address = $this->user->addresses()->where('status', 1)->select();
        $data['data'] = $address;
        $data['code'] = 0;
        return $this->autoAjax($data);
    }


    /**
     * 我的地址
     */
    public function get()
    {
        $id = $this->request->get('id', '');
        if ($id) {
            $address = $this->user->addresses()->where('id', $id)->where('status', 1)->hidden(['update_time', 'create_time', 'delete_time'])->find();
        } else {
            $address = $this->user->addresses()->order('is_default DESC')->where('status', 1)->hidden(['update_time', 'create_time', 'delete_time'])->find();
        }
        if ($address) {
            $data['code'] = 0;
            $data['data'] = $address;
        } else {
            $data['code'] = 1;
            $data['message'] = '地址不存在';
        }
        return $this->autoAjax($data);
    }

    public function default()
    {
        $address = $this->user->addresses()->order('is_default DESC')->where('status', 1)->hidden(['update_time', 'create_time', 'delete_time'])->find();
        if ($address) {
            $data['address'] = $address;
        }else{
            $data['address'] = null;
        }
        $data['size'] = ['XXL','XL','L'];
        return $this->successAjax($data);
    }

    /**
     * 更新/添加地址
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $event = new AddressEvent();
            $data = $event->update($this->request->post(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return $this->autoAjax($data);
    }
}
